package com.ruoyi.finance.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.finance.mapper.VoucherTemplateTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.finance.domain.TVoucherTemplate;
import com.ruoyi.finance.service.ITVoucherTemplateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 凭证模板Controller
 * 
 * @author ruoyi
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/template")
public class TVoucherTemplateController extends BaseController
{
    @Autowired
    private ITVoucherTemplateService tVoucherTemplateService;

    @Autowired
    VoucherTemplateTypeMapper voucherTemplateTypeMapper;

    /**
     * 查询凭证模板列表
     */
    @RequiresPermissions("finance:template:list")
    @GetMapping("/list")
    public TableDataInfo list(TVoucherTemplate tVoucherTemplate)
    {
        startPage();
        List<TVoucherTemplate> list = tVoucherTemplateService.selectTVoucherTemplateList(tVoucherTemplate);
        return getDataTable(list);
    }

    /**
     * 导出凭证模板列表
     */
    @RequiresPermissions("finance:template:export")
    @Log(title = "凭证模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TVoucherTemplate tVoucherTemplate)
    {
        List<TVoucherTemplate> list = tVoucherTemplateService.selectTVoucherTemplateList(tVoucherTemplate);
        ExcelUtil<TVoucherTemplate> util = new ExcelUtil<TVoucherTemplate>(TVoucherTemplate.class);
        util.exportExcel(response, list, "凭证模板数据");
    }

    /**
     * 获取凭证模板详细信息
     */
    @RequiresPermissions("finance:template:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tVoucherTemplateService.selectTVoucherTemplateById(id));
    }

    /**
     * 新增凭证模板
     */
    @RequiresPermissions("finance:template:add")
    @Log(title = "凭证模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TVoucherTemplate tVoucherTemplate)
    {
        return toAjax(tVoucherTemplateService.insertTVoucherTemplate(tVoucherTemplate));
    }

    /**
     * 修改凭证模板
     */
    @RequiresPermissions("finance:template:edit")
    @Log(title = "凭证模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVoucherTemplate tVoucherTemplate)
    {
        return toAjax(tVoucherTemplateService.updateTVoucherTemplate(tVoucherTemplate));
    }

    /**
     * 删除凭证模板
     */
    @RequiresPermissions("finance:template:remove")
    @Log(title = "凭证模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tVoucherTemplateService.deleteTVoucherTemplateByIds(ids));
    }


    @GetMapping("/selectVoucherType")
    public AjaxResult selectVoucherType()
    {
    	return success(voucherTemplateTypeMapper.selectList(null));
    }

}
